package cn.iocoder.yudao.module.member.controller.admin.auth;

import cn.iocoder.yudao.module.member.dal.dataobject.auth.MemberShopAuthDO;
import cn.iocoder.yudao.module.member.enums.MemberAuthBizTypeEnum;
import cn.iocoder.yudao.module.member.enums.MemberAuthShopBizTypeEnum;
import cn.iocoder.yudao.module.member.service.auth.DayuAuthService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.auth.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.auth.MemberAuthDO;
import cn.iocoder.yudao.module.member.convert.auth.MemberAuthConvert;


@Tag(name = "管理后台 - 会员认证大愚灸术学员")
@RestController
@RequestMapping("/member/auth")
@Validated
public class MemberAuthController {

    @Resource
    private DayuAuthService dayuAuthService;

    @PostMapping("/create")
    @Operation(summary = "创建会员认证大愚灸术学员")
    @PreAuthorize("@ss.hasPermission('member:auth:create')")
    public CommonResult<Long> createAuth(@Valid @RequestBody MemberAuthCreateReqVO createReqVO) {
        return success(dayuAuthService.createAuth(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员认证大愚灸术学员")
    @PreAuthorize("@ss.hasPermission('member:auth:update')")
    public CommonResult<Boolean> updateAuth(@Valid @RequestBody MemberAuthUpdateReqVO updateReqVO) {
        dayuAuthService.updateAuth(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员认证大愚灸术学员")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:auth:delete')")
    public CommonResult<Boolean> deleteAuth(@RequestParam("id") Long id) {
        dayuAuthService.deleteAuth(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员认证大愚灸术学员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:auth:query')")
    public CommonResult<MemberAuthRespVO> getAuth(@RequestParam("id") Long id) {
        MemberAuthDO auth = dayuAuthService.getAuth(id);
        return success(MemberAuthConvert.INSTANCE.convert(auth));
    }

    @GetMapping("/list")
    @Operation(summary = "获得会员认证大愚灸术学员列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:auth:query')")
    public CommonResult<List<MemberAuthRespVO>> getAuthList(@RequestParam("ids") Collection<Long> ids) {
        List<MemberAuthDO> list = dayuAuthService.getAuthList(ids);
        return success(MemberAuthConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员认证大愚灸术学员分页")
    @PreAuthorize("@ss.hasPermission('member:auth:query')")
    public CommonResult<PageResult<MemberAuthRespVO>> getAuthPage(@Valid MemberAuthPageReqVO pageVO) {
        PageResult<MemberAuthDO> pageResult = dayuAuthService.getAuthPage(pageVO);
        return success(MemberAuthConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员认证大愚灸术学员 Excel")
    @PreAuthorize("@ss.hasPermission('member:auth:export')")
    @OperateLog(type = EXPORT)
    public void exportAuthExcel(@Valid MemberAuthExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MemberAuthDO> list = dayuAuthService.getAuthList(exportReqVO);
        // 导出 Excel
        List<MemberAuthExcelVO> datas = MemberAuthConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "会员认证大愚灸术学员.xls", "数据", MemberAuthExcelVO.class, datas);
    }

    @PostMapping("/reject")
    @Operation(summary = "拒绝审核，大愚灸术学员认证")
    public CommonResult<Boolean> rejectMemberAuth(@RequestBody @Valid MemberAuthAuditReqVO reqVO){
        dayuAuthService.auditMemberAuth(reqVO.getId(), MemberAuthBizTypeEnum.REJECT, reqVO.getReason());
        return success(true);
    }

    @PostMapping("/approve")
    @Operation(summary = "审核通过， 大愚灸术学员认证")
    public CommonResult<Boolean> approveMemberAuth(@RequestBody @Valid MemberAuthAuditReqVO reqVO) {
        dayuAuthService.auditMemberAuth(reqVO.getId(), MemberAuthBizTypeEnum.SUCCESS, "");
        return success(true);
    }

    @PostMapping("/shop/reject")
    @Operation(summary = "拒绝审核，学员认证店铺")
    public CommonResult<Boolean> rejectMemberShopAuth(@RequestBody @Valid MemberAuthAuditReqVO reqVO){
        dayuAuthService.auditMemberShopAuth(reqVO.getId(), MemberAuthShopBizTypeEnum.REJECT, reqVO.getReason());
        return success(true);
    }

    @PostMapping("/shop/approve")
    @Operation(summary = "审核通过， 学员认证店铺")
    public CommonResult<Boolean> approveMemberShopAuth(@RequestBody @Valid MemberAuthAuditReqVO reqVO) {
        dayuAuthService.auditMemberShopAuth(reqVO.getId(), MemberAuthShopBizTypeEnum.SUCCESS, "");
        return success(true);
    }

    @GetMapping("/shop/page")
    @Operation(summary = "获得会员认证大愚灸术学员分页")
    @PreAuthorize("@ss.hasPermission('member:auth:query')")
    public CommonResult<PageResult<MemberShopAuthRespVO>> getAuthPage(@Valid MemberShopAuthPageReqVO pageVO) {
        PageResult<MemberShopAuthDO> pageResult = dayuAuthService.getMemberShopAuthPage(pageVO);
        return success(MemberAuthConvert.INSTANCE.convertShopPage(pageResult));
    }

    @PostMapping("/shop/create")
    @Operation(summary = "创建会员认证店铺")
    @PreAuthorize("@ss.hasPermission('member:auth:create')")
    public CommonResult<Long> createShopAuth(@Valid @RequestBody MemberShopAuthCreateReqVO createReqVO) {
        return success(dayuAuthService.createShopAuth(createReqVO));
    }
}
